package com.emeraldparser.util;

import static com.emeraldparser.util.Collections3.set;
import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Collections2.transform;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

/**
 * A Map whose keys and values are derived from the elements of a collection.
 * 
 * @author Adam Paynter
 *
 * @param <E> The type of element in the backing collection
 * @param <K> The type of key in the map
 * @param <V> The type of value in the map
 */
public abstract class CollectionBasedMap<E, K, V> extends AbstractMap<K, V> {

	/**
	 * Returns the collection from which keys and values are derived.
	 * 
	 * @return
	 */
	protected abstract Collection<E> collection();

	/**
	 * Returns the key associated with the collection element.
	 * 
	 * @param element
	 * @return
	 */
	protected abstract K key(E element);

	/**
	 * Returns the value associated with the key and associated collection elements.
	 * 
	 * @param key
	 * @param filteredElements The <em>view</em> of the subset of the collection whose elements all share the given key.
	 * @return
	 */
	protected abstract V value(Object key, Collection<E> filteredElements);

	/**
	 * Returns elements to be added to the collection based on the given key and value.
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	protected abstract Iterable<? extends E> elements(K key, V value);

	private Predicate<E> elementKeyPredicate(final Object key) {
		return new Predicate<E>() {
			@Override
			public boolean apply(E element) {
				return Objects.equal(key, key(element));
			}
		};
	}

	private Function<E, K> keyFunction() {
		return new Function<E, K>() {
			@Override
			public K apply(E value) {
				return key(value);
			}
		};
	}

	@Override
	public int size() {
		return keySet().size();
	}

	@Override
	public boolean isEmpty() {
		return collection().isEmpty();
	}

	private Collection<E> elements(Object key) {
		return elements(elementKeyPredicate(key));
	}

	private Collection<E> elements(Predicate<E> predicate) {
		return filter(collection(), predicate);
	}

	@Override
	public boolean containsKey(Object key) {
		return keys().contains(key);
	}

	@Override
	public V get(Object key) {
		return value(key, elements(key));
	}

	@Override
	public void clear() {
		collection().clear();
	}

	@Override
	public Set<K> keySet() {
		return set(keys());
	}

	private Collection<K> keys() {
		return transform(collection(), keyFunction());
	}

	@Override
	public Set<Map.Entry<K, V>> entrySet() {
		return set(entries());
	}

	private Collection<Map.Entry<K, V>> entries() {
		return transform(
			keySet(),
			new Function<K, Map.Entry<K, V>>() {
				@Override
				public Map.Entry<K, V> apply(K key) {
					return ImmutableMapEntry.create(key, value(key, elements(key)));
				}
			}
		);
	}

	@Override
	public V put(K key, V value) {
		V oldValue = value(key, Sets.newHashSet(elements(key)));
		Iterables.addAll(collection(), elements(key, value));
		return oldValue;
	}

	@Override
	public void putAll(Map<? extends K, ? extends V> m) {
		for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
			Iterables.addAll(collection(), elements(entry.getKey(), entry.getValue()));
		}
	}

	@Override
	public V remove(Object key) {
		Predicate<E> predicate = elementKeyPredicate(key);
		
		V oldValue = value(key, Sets.newHashSet(elements(predicate)));
		Iterables.removeIf(collection(), predicate);
		return oldValue;
	}

}
